package com.cn.ftp;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Map.Entry;

/**
 * FUN: Ftp的创建工厂
 *
 * @author sushy
 * @date 2016-3-23 
 */
public class FtpFactory {

	private static final String cfgFile = "ftp.props";
	private static final String pref = "com.cn.ftp.";//# 和配置文件ftp.props内容的前缀一样
	public static Map<String, FtpInfo> ftpConfigs = new HashMap<String, FtpInfo>();

	/**
	 * 根据配置实例化FtpUtil对象
	 */
	static {
		init();
	}

	private static void init() {
		InputStream in = Thread.currentThread().getContextClassLoader()
				.getResourceAsStream(cfgFile);
		if (in == null) {
			return;
		}

		Properties cfgs = new Properties();
		try {
			cfgs.load(in);

			// 初始化参数
			String cKey = null;
			String cValue = null;
			String modKey = null;
			String propKey = null;
			StringTokenizer st = null;
			int length = pref.length();
			for (Iterator<?> it = cfgs.entrySet().iterator(); it.hasNext();) {
				Entry<?, ?> e = (Entry<?, ?>) it.next();
				cKey = (String) e.getKey();
				cValue = (String) e.getValue();
				if (!cKey.startsWith(pref)) {
					continue;
				}

				// 获取参数名
				cKey = cKey.substring(length);
				st = new StringTokenizer(cKey, ".");
				if (st.hasMoreTokens()) {
					modKey = st.nextToken();
				}
				if (st.hasMoreTokens()) {
					propKey = st.nextToken();
				}

				if (propKey != null) {
					FtpInfo ftpInfo;
					if (ftpConfigs.containsKey(modKey)) {
						ftpInfo = (FtpInfo) ftpConfigs.get(modKey);
					} else {
						ftpInfo = new FtpInfo();
						ftpInfo.setKey(modKey);
						ftpConfigs.put(modKey, ftpInfo);
					}

					if ("user".equals(propKey)) {// 用户名
						ftpInfo.setUser(cValue);
					} else if ("password".equals(propKey)) {// 登录密码(暂时不考虑密码安全问题)
						ftpInfo.setPassword(cValue);
					} else if ("ip".equals(propKey)) {// 登录IP地址
						ftpInfo.setIp(cValue);
					} else if ("port".equals(propKey)) {// 登录端口
						int port = Integer.valueOf(cValue);
						if (port > 0) {
							ftpInfo.setPort(port);
						}
					} else if ("postfix".equals(propKey)) {// 上传文件时临时文件后缀名
						ftpInfo.setPostfix(cValue);
					} else if ("workBasePath".equals(propKey)) {// ftp默认工作路径
						ftpInfo.setWorkBasePath(cValue);
					}
				}
			}

			// 根据配置参数创建FtpUtil对象
			for (Iterator<?> it = ftpConfigs.entrySet().iterator(); it
					.hasNext();) {
				Entry<?, ?> e = (Entry<?, ?>) it.next();
				FtpInfo ftpInfo = (FtpInfo) e.getValue();
				createFtpUtil(ftpInfo);
			}
		} catch (Throwable e) {
			System.out.println(cfgFile + "配置文件错误或不存在：" + e.getMessage());
		}
	}

	/**
	 * 
	 * 创建一个FtpUtil的实例化对象
	 * 
	 * @param ftpInfo
	 * @return
	 */
	public static FtpUtil createFtpUtil(FtpInfo ftpInfo) {
		/** 获取存储对象的实例 */
		FtpConnectPool ftpPool = FtpConnectPool.getInstance();

		/** 从HashMap中获取一个连接的实例化对象 */
		FtpUtil ftpUtil = ftpPool.getFtpUtil(ftpInfo.getKey());
		if (ftpUtil == null) {
			try {
				/** 该连接不存在于HashMap中,实例化一个连接将此连接添加到HashMap中 */
				ftpUtil = new FtpUtil(ftpInfo);
				ftpPool.addFtpUtil(ftpInfo.getKey(), ftpUtil);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return ftpUtil;
	}

	/**
	 * 获取FtpUtil的实例化对象
	 * 
	 * @param key
	 * @return
	 */
	public static FtpUtil getFtpUtil(String key) {
		return FtpConnectPool.getInstance().getFtpUtil(key);
	}

}
